<template>
    <div>
        <el-form ref="form" :model="obj.data" label-width="100px">
            <el-form-item label="选择设备" prop="device_id" :rules="[formRulesMixin.notNull]">
                <el-select v-model="obj.data.device_id" placeholder="选择设备" class="search_select" clearable>
                    <el-option v-for="(item,index) in obj.device_all" :key="index" :label="item.name" :value="item.device_id" />
                </el-select>
            </el-form-item>
            <el-form-item label="选择巡检人" prop="userid" :rules="[formRulesMixin.notNull]">
                <el-select v-model="obj.data.userid" placeholder="选择巡检人" class="search_select" clearable>
                    <el-option v-for="(item,index) in obj.property_all" :key="index" :label="item.name" :value="item.property_id" />
                </el-select>
            </el-form-item>
            <el-form-item label="巡检时间" prop="totime" :rules="[formRulesMixin.notNull]">
                <DatePicker v-model:value="obj.data.totime" type="datetime" format="YYYY-MM-DD HH:mm" valueformat="YYYY-MM-DD HH:mm" :width="160" :disabledDate="setDate" />
            </el-form-item>
            <el-form-item class="dialog-btns-item">
                <div class="dialog-btns">
                    <span class="btn clear" @click="$emit('close')">取消</span>
                    <span class="btn sub ml10" @click.prevent="upData">提交</span>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRaw,inject } from 'vue'
    import DatePicker from '@/components/date_picker.vue'
    let { proxy } = getCurrentInstance()
    let obj = reactive({
        data:{
            inspection_id: inject("id").value || 0,
            device_id: "",
            userid: "",
            totime:"",
        },
        property_all:[],
        device_all:[],
    })

    let setDate = d => { return d <= proxy.$utils.dateAddDay(new Date(), -1) }
    let init = async () => {
        obj.device_all = await proxy.$utils.getDevice(proxy) 
        obj.property_all = await proxy.$utils.getProperty(proxy, 4) //4表示维修
        if(obj.data.inspection_id > 0){
            proxy.$ajax({
                url: 'inspection/get',
                data: obj.data,
                bfLoading: true
            }).then(d => {
                if(d.success){
                    obj.data = {...obj.data, ...d.msg}
                }else{
                    proxy.elMessageMixin(d.err, 'error')
                }
            }).catch(() => { })
        }
    }
    init()
    let upData = () => {
        proxy.$refs['form'].validate(valid => {
            if (valid) {
                proxy.$ajax({
                    url: 'inspection/design',
                    data: obj.data,
                    bfLoading: true,
                }).then(d => {
                    if(d.success){
                        proxy.elMessageMixin(`${obj.data.inspection_id>0?'修改':"添加"}成功`, 'success')
                        proxy.$emit('init')
                        proxy.$emit('close')
                    }else{
                        proxy.elMessageMixin(d.err, 'error')
                    }
                }).catch(() => { })
            }else{
                return false
            }
        })
    }
</script>

<style scoped>
</style>